package org.opens.javaskill.design;

import org.junit.Test;

/**
 * 功能:
 * 抽象工厂模式实例.
 */
public class AbstractFactoryPattern {

    @Test
    public void test1() throws Exception {
        AbstractFactory wheelFactory = CatFactory.getFactory("wheel");
        AbstractFactory lightFactory = CatFactory.getFactory("light");

        Wheel wheel1 = wheelFactory.getWheel(1);
        Wheel wheel2 = wheelFactory.getWheel(2);
        System.out.println(wheel1.makeWheel());
        System.out.println(wheel2.makeWheel());

        Light light1 = lightFactory.getLight(1);
        Light light2 = lightFactory.getLight(2);
        System.out.println(light1.makeLight());
        System.out.println(light2.makeLight());
    }

}

class CatFactory {

    public static AbstractFactory getFactory(String factoryName) throws Exception {
        if("wheel".equals(factoryName)) {
            return new WheelFactory();
        }
        if("light".equals(factoryName)) {
            return new LightFactory();
        }
        throw new Exception("没有这个工厂");
    }

}

abstract class AbstractFactory {

    public abstract Wheel getWheel(int productionLineNumber) throws Exception;

    public abstract Light getLight(int productionLineNumber) throws Exception;

}

interface Wheel {

    public abstract String makeWheel();

}

class ProductionLineOne implements Wheel {

    @Override
    public String makeWheel() {
        return "车轮生产线1-车轮";
    }
}

class ProductionLineTwo implements Wheel {

    @Override
    public String makeWheel() {
        return "车轮生产线2-车轮";
    }
}

class WheelFactory extends AbstractFactory {

    @Override
    public Wheel getWheel(int productionLineNumber) throws Exception {
        if(productionLineNumber == 1) {
            return new ProductionLineOne();
        }
        if(productionLineNumber == 2) {
            return new ProductionLineTwo();
        }
        throw new Exception("没有这个类");
    }

    @Override
    public Light getLight(int productionLineNumber) {
        throw new RuntimeException("不支持的操作");
    }
}

interface Light {

    public abstract String makeLight();

}

class LightProductionLineOne implements Light {

    @Override
    public String makeLight() {
        return "车灯生产线1-车灯";
    }
}

class LightProductionLineTwo implements Light {

    @Override
    public String makeLight() {
        return "车灯生产线2-车灯";
    }
}

class LightFactory extends AbstractFactory {

    @Override
    public Wheel getWheel(int productionLineNumber) throws Exception {
        throw new RuntimeException("不支持的操作");
    }

    @Override
    public Light getLight(int productionLineNumber) throws Exception {
        if(productionLineNumber == 1) {
            return new LightProductionLineOne();
        }
        if(productionLineNumber == 2) {
            return new LightProductionLineTwo();
        }
        throw new Exception("没有这个类");
    }
}




